//
//  WYHonorCell.m
//  Boss
//
//  Created by admin on 2018/7/5.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYHonorCell.h"
#import "WYHonnorCollectionViewCell.h"
#import "CJFirmHonorModel.h"
@interface WYHonorCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *collectionViewHeight;




@end
@implementation WYHonorCell
-(void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    [self reloadCell];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUpUI];
}
-(void)setUpUI
{
    //self.collectionView.backgroundColor = [UIColor purpleColor];
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake((kScreenWidth-32-15)/2.0, (kScreenWidth-32-15)/2.0*0.73);
    
    
    //横向最小距离
    layout.minimumInteritemSpacing=10.0f;
    layout.minimumLineSpacing=10.0f;
    //cell距离上  左  下 右 的距离
    layout.sectionInset=UIEdgeInsetsMake(10, 16, 10, 16);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [self.collectionView setCollectionViewLayout:layout];
    self.collectionView.dataSource= self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"WYHonnorCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"WYHonnorCollectionViewCell"];
    
//    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.bottom.top.mas_equalTo(0);
//        make.height.mas_equalTo(130);
//    }];
    
    
}

- (void)reloadCell{
    [self.collectionView reloadData];
    if (self.dataArray.count) {
        self.collectionViewHeight.constant = ceil(self.dataArray.count/2.0)*((kScreenWidth-32-15)/2.0*0.73+20);
        NSLog(@"%f",self.collectionView.height);
    }
//    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.height.equalTo(@(self.collectionView.collectionViewLayout.collectionViewContentSize.height));
//    }];
//    if (self.collectionViewHeight.constant != self.collectionView.collectionViewLayout.collectionViewContentSize.height) {
//        self.collectionViewHeight.constant = self.collectionView.collectionViewLayout.collectionViewContentSize.height;
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"didChangeHeight" object:nil];
//        if ([self.delegate respondsToSelector:@selector(didChangeHeightWithHonor:)]) {
//            [self.delegate didChangeHeightWithHonor:self];
//        }
//    }else{
//
//
//    }
    
   
    // [self.delegate didChangeCell:self];
}



-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WYHonnorCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"WYHonnorCollectionViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYHonnorCollectionViewCell" owner:nil options:nil].firstObject;
    }
    CJFirmHonorModel *model = self.dataArray[indexPath.row];
    [cell.honnorImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@!default",model.url]] placeholderImage:[UIImage imageNamed:@""]];
    
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WYHonnorCollectionViewCell *cell =(WYHonnorCollectionViewCell *) [collectionView cellForItemAtIndexPath:indexPath];
    NSString *str = [NSString stringWithFormat:@"%@!default",self.dataArray[indexPath.row]];
   // [self.dataArray removeObjectAtIndex:indexPath.row];
    if ([self.delegate respondsToSelector:@selector(didSelectImageViewWithImage:WithImageUrl:WithDataArray:WithIndex:)]) {
        [self.delegate didSelectImageViewWithImage:cell.honnorImageView.image WithImageUrl:str WithDataArray:self.dataArray WithIndex:indexPath];
    }
    
    
}
@end
